﻿using System;
using System.Text;

// All the drawing takes part in this class.
public class ConsoleRenderer : IRenderer
{
    public void RenderBoard(GameBoard gameBoard)
    {
        for (int row = 0; row < gameBoard.Rows; row++)
        {
            for (int col = 0; col < gameBoard.Cols; col++)
            {
                Coords2D currentSquare = new Coords2D(row, col);
                Console.SetCursorPosition((col + 1) * gameBoard.ColOffset, (row + 1) * gameBoard.RowOffset);

                if (gameBoard[currentSquare].IsOpened == true)
                {
                    Console.ForegroundColor = gameBoard[currentSquare].Color;
                    Console.Write(gameBoard[currentSquare].Image[0, 0]);
                }
                else if (gameBoard[currentSquare].IsMarkedAsMine == true)
                {
                    Console.ForegroundColor = Square.MarkedAsMineColor;
                    Console.Write(Square.MarkedAsMineImage[0, 0]);
                }
                else
                {
                    Console.ForegroundColor = Square.UnopenedSquareColor;
                    Console.Write(Square.UnopenedSquareImage[0, 0]);
                }
            }
        }

        ClearPositionAndColor();
    }

    public void RenderPlayerInitially(Player player, int rowOffset, int colOffset)
    {
        Console.ForegroundColor = player.Color;
        int colStart = (player.CoordsNew.Col + 1) * colOffset - 1;
        int colEnd = colStart + player.Image.GetLength(0);

        for (int col = colStart; col <= colEnd; col++)
        {
            Console.SetCursorPosition(col, (player.CoordsNew.Row + 1) * rowOffset - 1);
            Console.Write(player.Image[0, col - colStart]);

            Console.SetCursorPosition(col, (player.CoordsNew.Row + 1) * rowOffset + 1);
            Console.Write(player.Image[1, col - colStart]);
        }

        ClearPositionAndColor();
    }

    public void RenderPlayer(Player player, int rowOffset, int colOffset)
    {
        // Move player as two seperate parts. Player is firstly realocated to a buffer position to make sure there are no overlapping.
        int bufferRow = 0;
        int bufferCol = 0;
        int oldCol = (player.CoordsOld.Col + 1) * colOffset;
        int oldRow = (player.CoordsOld.Row + 1) * rowOffset;
        int imageLength = player.Image.GetLength(0) + 1;
        int newCol = (player.CoordsNew.Col + 1) * colOffset;
        int newRow = (player.CoordsNew.Row + 1) * rowOffset;

        Console.MoveBufferArea(
            oldCol - 1,
            oldRow - 1,
            imageLength,
            1,
            bufferCol,
            bufferRow);

        Console.MoveBufferArea(
            oldCol - 1,
            oldRow + 1,
            imageLength,
            1,
            newCol - 1,
            newRow + 1);

        Console.MoveBufferArea(
            bufferCol,
            bufferRow,
            imageLength,
            1,
            newCol - 1,
            newRow - 1);

        ClearPositionAndColor();
    }

    public void RenderGameOver(GameBoard gameBoard)
    {
        for (int row = 0; row < gameBoard.Rows; row++)
        {
            for (int col = 0; col < gameBoard.Cols; col++)
            {
                Coords2D currentSquare = new Coords2D(row, col);

                if (gameBoard[currentSquare] is MineSquare)
                {
                    Console.SetCursorPosition((col + 1) * gameBoard.ColOffset, (row + 1) * gameBoard.RowOffset);

                    Console.ForegroundColor = gameBoard[currentSquare].Color;
                    Console.Write(gameBoard[currentSquare].Image[0, 0]);
                }
            }
        }

        Console.SetCursorPosition(gameBoard.GameOverLocation.Col, gameBoard.GameOverLocation.Row);
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("GAME OVER!!!");
        ClearPositionAndColor();
    }

    public void RenderPlayTime(Coords2D location, TimeSpan time)
    {
        Console.SetCursorPosition(location.Col, location.Row);
        Console.Write(time.Minutes + ":" + time.Seconds);

        ClearPositionAndColor();
    }

    private void ClearPositionAndColor()
    {
        Console.SetCursorPosition(0, 0);
        Console.ForegroundColor = ConsoleColor.White;
    }
}
